package com.zscat.mallplus.utils;

import com.zscat.mallplus.vo.MealDate;
import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;

/**
 * Created by hanfei on 16/5/12.
 */
public class DateUtil {
    private static DateFormat df;

    static {
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static String getFirstDayOfCurrentMonth(String format) {
        SimpleDateFormat sf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        gcLast.set(Calendar.HOUR_OF_DAY, 0);
        gcLast.set(Calendar.MINUTE, 0);
        gcLast.set(Calendar.SECOND, 0);
        String day_first = sf.format(gcLast.getTime());
        return day_first;
    }

    public static Date parse(String dateStr) {
        try {
            Date dateTime = df.parse(dateStr);
            return dateTime;
        } catch (ParseException e) {
            throw new RuntimeException("日期格式转换异常!");
        }
    }

    public static List<MealDate> getDateForMeal() {
        List<MealDate> mealDates = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            MealDate mealDate = new MealDate();
            LocalDate now = LocalDate.now();
            now = now.plusDays(i);
            DayOfWeek weekday = now.getDayOfWeek();
            String date = StringUtils.replace(now.toString(), "-", "/");
            String[] dates = StringUtils.split(date, "/");
            String dateFormat = dates[1] + "月" + dates[2] + "日";
            mealDate.setDate(date);
            mealDate.setDateFormat(dateFormat);
            mealDate.setWeek(weekToChinese(weekday.toString()));
            mealDates.add(mealDate);
        }
        return mealDates;
    }

    public static Map<String,String> getTimeArea(int type){
        Map<String,String> result = new HashMap<>();
        if (type==0){
            result.put("startTime",dateToStr(new Date(),"yyy-MM-dd")+" 00:00:00");
            result.put("endTime",dateToStr(new Date(),"yyy-MM-dd")+" 23:59:59");
        }
        if (type==1){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE,-1);
            result.put("startTime",dateToStr(calendar.getTime(),"yyyy-MM-dd")+" 00:00:00");
            result.put("endTime",dateToStr(calendar.getTime(),"yyyy-MM-dd")+" 23:59:59");
        }
        if (type==2){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            Date startDate = calendar.getTime();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date endDate = calendar.getTime();
            result.put("startTime",dateToStr(startDate,"yyyy-MM-dd")+" 00:00:00");
            result.put("endTime",dateToStr(endDate,"yyyy-MM-dd")+" 23:59:59");
        }
        if (type==3){
            result.put("startTime","");
            result.put("endTime","");
        }
        return result;
    }

    public static void main(String[] args) {
        getTimeArea(2);
    }

    private static String weekToChinese(String week) {
        if (week.equalsIgnoreCase("Monday")){
            return "星期一";
        }else if (week.equalsIgnoreCase("Tuesday")){
            return "星期二";
        }else if (week.equalsIgnoreCase("Wednesday")){
            return "星期三";
        }else if (week.equalsIgnoreCase("Thursday")){
            return "星期四";
        }else if (week.equalsIgnoreCase("Friday")){
            return "星期五";
        }else if (week.equalsIgnoreCase("Saturday")){
            return "星期六";
        }else {
            return "星期日";
        }
    }

    public static String dateToStr(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        String s = "";

        try {
            s = format.format(date);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return s;
    }

    public static Date parseByFormat(String dateStr, String format) {
        try {
            DateFormat dateFormat = new SimpleDateFormat(format);
            Date dateTime = dateFormat.parse(dateStr);
            return dateTime;
        } catch (ParseException e) {
            throw new RuntimeException("日期格式转换异常!");
        }
    }

    public static Date parse(String dateStr, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            Date dateTime = df.parse(dateStr);
            return dateTime;
        } catch (ParseException e) {
            throw new RuntimeException("日期格式转换异常!");
        }
    }

    public static String dateToString(Date date) {
        return df.format(date);
    }

    public static String dateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * unix时间戳转换为dateFormat
     *
     * @param beginDate
     * @return
     */
    public static String timestampToDate(String beginDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(beginDate)));
        return sd;
    }

    /**
     * 自定义格式时间戳转换
     *
     * @param beginDate
     * @return
     */
    public static String timestampToDate(String beginDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String sd = sdf.format(new Date(Long.parseLong(beginDate)));
        return sd;
    }

    /**
     * 两个时间段是否有相交
     *
     * @return true 时间重叠 false 时间不重叠
     */
    public static Boolean isIntersect(String s1, String e1, String s2, String e2) {
        if (!(s1.compareTo(e2) >= 0 || e1.compareTo(s2) <= 0)) {
            return true;
        }
        return false;
    }

    /**
     * 比较时间差
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int timeDiff(String date1, String date2) {
        Date d1 = new Date(date1);
        Date d2 = new Date(date2);
        return (int) (d1.getTime() - d2.getTime()) / 60000;
    }

    /**
     * 将字符串转为时间戳
     *
     * @param user_time
     * @return
     */
    public static String dateToTimestamp(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return re_time;
    }


    public static String dateToTimestamp(Date time) {
        long l = time.getTime();
        String str = String.valueOf(l);
        String re_time = str.substring(0, 10);
        return re_time;
    }

    public static Date nowTime() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        String nowTime = sf.format(new Date());
        Date createTime = null;
        try {
            createTime = sf.parse(nowTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return createTime;
    }

    /**
     * yyyy-mm-dd格式字符串转时间
     *
     * @param strDate
     * @return
     */
    public static Date StringToDate(String strDate) {
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //必须捕获异常
        try {
            date = simpleDateFormat.parse(strDate);
        } catch (ParseException px) {
            px.printStackTrace();
        }
        return date;
    }

    public static Date StringToDate(String strDate, String format) {
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        //必须捕获异常
        try {
            date = simpleDateFormat.parse(strDate);
        } catch (ParseException px) {
            px.printStackTrace();
        }
        return date;
    }

    /**
     * 计算时间差
     *
     * @return 天数
     */
    public static int differentDaysByMillisecond(Date targetTime, Date nowTime) {
        int days = (int) ((targetTime.getTime() - nowTime.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 计算时间差
     *
     * @return 秒数
     */
    public static int differentSecondByMillisecond(Date targetTime, Date nowTime) {
        int days = (int) ((targetTime.getTime() - nowTime.getTime()) / 1000);
        return days;
    }

    /**
     * 获取进行加减后的时间
     *
     * @param startTime
     * @param minute    需要加减的时间
     * @return
     */
    public static String getResultTime(Date startTime, int minute) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(startTime);
        nowTime.add(Calendar.MINUTE, minute);
        return sf.format(nowTime.getTime());
    }


    public static Date addMonth(int offset) {
        Date d = new Date();
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(d);
        gc.add(2, offset);//2代表月份，1代表在当前的日期添加一个月
        gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
        return gc.getTime();
    }

    public static Date addDays(final Date date, final int amount) {
        Date res = add(date, Calendar.DAY_OF_MONTH, amount);
        return parse(dateToString(res), "yyyy-MM-dd");
    }

    private static Date add(final Date date, final int calendarField, final int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        final Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }
}
